package thread;


import java.util.concurrent.Semaphore;

public class ThreadWork2 {

    public int count = 0;

    synchronized public void add() {
       count++;
    }

    public static void main(String[] args) throws InterruptedException{
        ThreadWork2 threadWork2 = new ThreadWork2();
        Semaphore semaphore = new Semaphore(2);
        semaphore.acquire(); //加锁
        Thread t1 = new Thread(() ->{
            for (int i = 0; i < 10000; i++) {
                threadWork2.add();
            }

        });
        semaphore.acquire();
        Thread t2 = new Thread(() ->{
            for (int i = 0; i < 10000; i++) {
                threadWork2.add();
            }
        });

        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(threadWork2.count);
    }

    /*public static void main(String[] args) {
        ThreadWork2 threadWork2 = new ThreadWork2();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                threadWork2.add();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                threadWork2.add();
            }
        });

        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(threadWork2.count);

    }*/
}

